package com.kaishengit.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.kaishengit.dao.DocumentDao;
import com.kaishengit.entity.Document;

public class DocumentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


			
			int start = 0;
			HttpSession session = request.getSession();
			if(session.getAttribute("sessionNum") == null){
				start = 0;
			}else{
				start = (Integer)session.getAttribute("sessionNum");
			}
			
			
			System.out.println(start);
			
			DocumentDao dao = new DocumentDao();
			
			
			response.setCharacterEncoding("UTF-8");
			List<Document> personList = dao.findByLimit(start);
			session.setAttribute("sessionNum", start + 4);
			
			JSONArray json = JSONArray.fromObject(personList);
			
			response.setContentType("application/json;charset=UTF-8");
			
			PrintWriter out = response.getWriter();
			String jsonString = json.toString();
			System.out.println(jsonString);
			out.print(jsonString);
			out.flush();
			out.close();
		
		
		
		
		
		
		
		
	}

}
